import axios from 'axios'
import { ElMessage } from 'element-plus'
// import { diffTokenTime } from '@/utils/auth'
// import store from '@/store'
const service = axios.create({
  baseURL: 'http://192.168.1.222:8000/api',
  timeout: 5000
})

// 请求头添加token
// service.interceptors.request.use(
//   (config) => {
//     if (localStorage.getItem('token')) {
//       if (diffTokenTime()) {
//         store.dispatch('app/logout')
//         return Promise.reject(new Error('token 失效了'))
//       }
//     }
//     config.headers.Authorization = localStorage.getItem('token')
//     return config
//   },
//   (error) => {
//     return Promise.reject(new Error(error))
//   }
// )

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    const { data, status } = response.data
    // console.log(response.data)
    if (status === 200 || status === 201) {
      return data
    } else {
      ElMessage.error(data.msg)
      return Promise.reject(new Error(data.msg))
    }
  },
  (error) => {
    console.log(error.response)
    error.response && ElMessage.error(error.response.data)
    return Promise.reject(new Error(error.response.data))
  }
)

export default service
